#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: xiaodun
@date: 2025/9/1820:17
"""

import pytest
from selenium.webdriver import Remote
from utils.driver_manager import DriverManager
# from dl_auto_test.pages.home_page import HomePage
from dl_auto_test.config.config import config
from dl_auto_test.pages.login_page import LoginPage
from dl_auto_test.pages.project_registration import Project_Registration


@pytest.fixture(scope="session")
def driver():
    driver_instance=DriverManager().create_driver()
    yield driver_instance
    driver_instance.quit()


@pytest.fixture(scope="function")
def login_page(driver):
    login_page_instance=LoginPage(driver)
    yield login_page_instance

    # if "login" not in driver.current_url:
    #     login_page_instance.open_login_page()


@pytest.fixture(scope="function")
def project_Registration(driver):
    Project_registration=Project_Registration(driver)
    Project_registration.login()
    Project_registration.switch_to_frame_leftFrame()
    Project_registration.Participate_in_registration_click()
    time.sleep(3)
    Project_registration.Project_registration_click()
    time.sleep(3)
    # Project_registration.exit_frame()
    # Project_registration.switch_to_frame_topFrame()
    yield Project_registration
    # Project_registration.exit_click()





#用例失败时截图保存
@pytest.hookimpl(tryfirst=True,hookwrapper=True)
def pytest_runtest_makereport(item,call):
    outcome=yield
    rep=outcome.get_result()
    if rep.when=="call" and rep.failed:
        driver_fixture=item.funcargs.get('driver',None)
        if driver_fixture is not None and isinstance(driver_fixture,Remote):
            screenshot_name=f"{item.name}_{call.when}.png"
            driver_fixture.save_screenshot(screenshot_name)
            print(f"测试失败，已保存截图：{screenshot_name}")